﻿using Common;
using Puzzle.Properties;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Puzzle {
    // 主窗口
    public partial class MainWindow: Form, Game.Callback {
        private readonly Game mGame;    // 游戏

        /**
         * 构造方法。
         */
        public MainWindow() {
            // 初始化控件
            this.InitializeComponent();
            
            // 创建游戏区域
            GameBoard gameBoard = new GameBoard(Controls, 0, MenuGame.Height, Const.GAME_BOARD_WIDTH, Const.GAME_BOARD_HEIGHT);

            // 创建游戏
            mGame = new Game(this, gameBoard.Drawer, gameBoard);
        }

        /**
         * "图片边框"菜单项的单击响应方法。
         * @param sender    事件发送者
         * @param e         事件相关信息
         */
        private void MenuSetBorder_Click(object sender, EventArgs e) {
            // 如果要设置的参数与当前游戏参数相同，则直接返回
            bool hasBorder = sender == MenuItemHasBorder;
            if (hasBorder == mGame.PictureHasBorder) {
                return;
            }

            // 设置边框
            mGame.PictureHasBorder = hasBorder;

            // 选中菜单项
            MenuItemHasBorder.Checked = hasBorder;
            MenuItemNoBorder.Checked = !hasBorder;
        }

        /**
         * "难度"菜单项的单击响应方法。
         * @param sender    事件发送者
         * @param e         事件相关信息
         */
        private void MenuSetDifficulty_Click(object sender, EventArgs e) {
            // 如果要设置的参数与当前游戏参数相同，则直接返回
            ToolStripMenuItem clickedMenuItem = (ToolStripMenuItem)sender;
            Game.Difficulty newDiff = Game.Difficulty.EASY;
            if (clickedMenuItem == MenuItemMedium) {
                newDiff = Game.Difficulty.MEDIUM;
            } else if (clickedMenuItem == MenuItemHard) {
                newDiff = Game.Difficulty.HARD;
            }
            if (newDiff == mGame.CurrentDifficulty) {
                return;
            }

            // 弹出询问对话框
            if (mGame.CurrentStatus != Game.Status.END) {
                DialogResult clickedButton = MessageBox.Show(Resources.IDS_RESET, " ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (clickedButton == DialogResult.No) {
                    ToolStripMenuItem MenuItem = (ToolStripMenuItem)sender;
                    MenuItem.Checked = false;
                    return;
                }
            }

            // 设置难度
            mGame.CurrentDifficulty = newDiff;

            // 选中菜单项
            MenuItemEasy.Checked = false;
            MenuItemMedium.Checked = false;
            MenuItemHard.Checked = false;
            clickedMenuItem.Checked = true;
        }

        /**
         * "换图片"菜单项的单击响应方法。
         * @param sender    事件发送者
         * @param e         事件相关信息
         */
        private void MenuItemSetPicture_Click(object sender, EventArgs e) {
            // 弹出询问对话框
            DialogResult clickedButton;
            if (mGame.CurrentStatus != Game.Status.END) {
                clickedButton = MessageBox.Show(Resources.IDS_RESET, " ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (clickedButton == DialogResult.No) {
                    return;
                }
            }

            // 弹出选择图片对话框
            OpenFileDialog fileDlg = new OpenFileDialog();
            fileDlg.Filter = "(*.bmp; *.gif; *.jpg; *.png)|*.bmp;*.gif;*.jpg;*.png";
            clickedButton = fileDlg.ShowDialog();
            if (clickedButton == DialogResult.Cancel) {
                return;
            }

            // 设置图片
            mGame.SetPicture(Image.FromFile(fileDlg.FileName));
        }

        /**
         * "开始"菜单项的单击响应方法。
         * @param sender    事件发送者
         * @param e         事件相关信息
         */
        private void MenuItemStart_Click(object sender, EventArgs e) {
            mGame.Start();
        }

        /**
         * 游戏结束事件的响应方法。
         */
        public void OnGameOver() {
            // 显示胜利提示信息
            MessageBox.Show(Resources.IDS_WON, " ", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
